package com.art8173.controller;

import com.art8173.pojo.Dept;
import com.art8173.pojo.Result;
import com.art8173.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理控制器
 */
@Slf4j
@RestController
public class DeptController {

//    private static final Logger LOG = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;
    @GetMapping("/depts")
    public Result list() {
        log.info("查询全部部门信息");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    @DeleteMapping("/depts")
    public Result delete( Integer id) {
        log.info("删除部门信息：{}", id);
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 新增部门 - POST http://localhost:8080/depts   请求参数：{"name":"研发部"}
     */
    @PostMapping("/depts")
    public Result save(@RequestBody Dept dept){
        log.info("新增部门信息：{}", dept);
        deptService.save(dept);
        return Result.success();
    }

    /**
     * 根据ID查询 - GET http://localhost:8080/depts/1
     */
    @GetMapping("/depts/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询部门信息：{}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    /**
     * 修改 - PUT http://localhost:8080/depts
     */
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        log.info("修改部门信息：{}", dept);
        deptService.update(dept);
        return Result.success();
    }
}